<?php

namespace Admin\Controller;

/** Description:
 * @Author: Tudo<997470395@qq.com>
 * @Created: 2015-2-13 下午1:19:13 */
use Common\Library\Util\Tree;

class DataController extends BaseController
{
	private $blogModel;
	public function __construct() {
		parent::__construct();
		$this->blogModel = D( 'Blog' );
	}
	public function add() {
		if (IS_POST) {
			$tags = array();
			$map = I( 'post.', null, 'trim' );
			if (isset( $map['tags'] )) {
				$tags = $map['tags'];
				unset( $map['tags'] );
			}
			if (isset( $map['__hash__'] )) unset( $map['__hash__'] );
			$map['add_time'] = time();
			$map['click'] = rand( 10, 50 );
			$map['uid'] = $this->gAdmin['id'];
			
			if ($this->blogModel->add( $map )) {
				$bid = $this->blogModel->getLastInsID();
				D( 'Tag' )->insertTags( $tags, $bid );
				echo $this->success( "添加成功", U( 'blog/index' ) );
			} else {
				echo $this->error( "添加失败" );
			}
			return;
		}
		$this->assign( 'categorys', $this->returnCate() );
		$this->display();
	}
	public function index() {
		$list = $this->blogModel->select();
		$this->assign('list',$list);
		$this->display();
	}
	public function category() {
		$this->assign( 'list', $this->returnCate() );
		$this->display();
	}
	private function returnCate() {
		$list = $this->lists( 'category', null, $order = "sort,id" );
		return Tree::unlimitedForLevel( $list );
	}
	/** ajax 实时请求tag** */
	public function getags() {
		$keyword = I( 'post.tag', '', 'trim' );
		if (empty( $keyword )) die( json_encode( array('status' => - 1) ) );
		$list = D( 'Tag' )->where( array('tag_name' => array('like','%'.$keyword.'%')) )->field( 'tag_name,id' )->order( 'sort' )->select();
		$data['status'] = 1;
		$data['list'] =array('id'=>'1','text'=>'1111','id'=>'2','text'=>'222');
		echo json_encode( $data );
	}
}